package com.zbfmq.xonfig;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/3/10  13:48
 * 描述:
 */
@Configuration
public class RabbitmqConfig {

    @Bean("queue")
    public Queue getQueue1(){
        return new Queue("TEST-QUEUE",false,false,true);
    }

    @Bean("queue2")
    public Queue getQueue2(){
        return new Queue("TEST-QUEUE2",false,false,true);
    }

    @Bean("exchange")
    public Exchange getExchange1(){
        //创建一个直连的交换器
        //Exchange build = ExchangeBuilder.directExchange("ZHI-LIAN").durable(false).autoDelete().build();
        //创建一个Topic交换器
        Exchange build = ExchangeBuilder.topicExchange("TOPIC-1").autoDelete().durable(false).build();
        //fanout交换器
        //Exchange build = ExchangeBuilder.fanoutExchange("FANOUT-1").autoDelete().durable(false).build();

        //header交换器
        HeadersExchange headersExchange = new HeadersExchange("headersExchange",false,true);
        return headersExchange;

        //return build;
    }


    @Bean("binding1")
    public Binding getBinding(Queue queue,@Qualifier("exchange") Exchange exchnage){
        //直连的bind
        //Binding zhiliankey = BindingBuilder.bind(queue).to(exchange).with("zhiliankey").noargs();
        //Topic绑定
        /*Binding binding = BindingBuilder.bind(queue).to(exchnage).with("A.*").noargs();*/
        //Binding binding = BindingBuilder.bind(queue).to(exchnage).with("A.#").noargs();
        Binding binding = BindingBuilder.bind(queue).to(exchnage).with("#.A.#").noargs();

        return binding;

    }

    /**
     * 作者: LCG
     * 日期: 2021/3/11  0:11
     * 描述: header交换器
     * @Param []
     * @Return org.springframework.amqp.core.HeadersExchange
     */
   /* @Bean("headersExchange")
    public HeadersExchange headerExchange(){
        HeadersExchange headersExchange = new HeadersExchange("HEADER-1", false, true);
        return headersExchange;
    }*/

    /**
     * 作者: LCG
     * 日期: 2021/3/10  23:53
     * 描述: 测试Header模式
     * @Param [queue2]
     * @Return org.springframework.amqp.core.Binding
     */
   /* @Bean("binding2")
    public Binding getBinding2(@Qualifier("queue2") Queue queue,HeadersExchange headersExchange){
        Map<String,Object> map=new HashMap<>();
        map.put("zhangsan","123");

        Binding binding = BindingBuilder.bind(queue).to(headersExchange).whereAll(map).match();
        return binding;
    }*/



    @Bean("headerExchnage")
    public HeadersExchange getHeaderExchange(){
        HeadersExchange headersExchange=new HeadersExchange("HEADER-1",false,true);
        return headersExchange;
    }

    @Bean("binding2")
    public Binding getHeaderBinding(@Qualifier("queue2") Queue queue,HeadersExchange headerExchnage){

        Map<String,Object> map=new HashMap<>();
        map.put("zhangsan","123");
        map.put("goodsId","456");

        Binding binding = BindingBuilder.bind(queue).to(headerExchnage).whereAll(map).match();

        return binding;
    }


}
